package ncut.shumei;

import ncut.shumei.dao.BookDao;
import ncut.shumei.entity.Book;
import ncut.shumei.entity.Student;
import ncut.shumei.entity.StudentBookQuery;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        try {
            // 初始化Spring容器
            ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
            BookDao bookDao = context.getBean("bookDao", BookDao.class);

            // 测试1：查询带"小"字的学生借阅"计算机网络教程"的学生信息
            System.out.println("\n=== 测试1：查询带'小'字的学生借阅'计算机网络教程'的学生信息 ===");
            StudentBookQuery query1 = new StudentBookQuery();
            query1.setStudentNameLike("小");
            query1.setBookName("计算机网络教程");
            List<Student> students = bookDao.findStudentsWhoReadNetwork(query1);
            System.out.println("查询结果：");
            students.forEach(student -> System.out.println("- " + student));

            // 测试2：查询带"小"字的学生借阅的图书信息
            System.out.println("\n=== 测试2：查询带'小'字的学生借阅的图书信息 ===");
            StudentBookQuery query2 = new StudentBookQuery();
            query2.setStudentNameLike("小");
            List<Book> books = bookDao.findBooksWithStudentNameFragment(query2);
            System.out.println("查询结果：");
            books.forEach(book -> System.out.println("- " + book));

            // 测试3：查询带"李"字的学生借阅特定图书的信息
            System.out.println("\n=== 测试3：查询带'李'字的学生借阅特定图书的信息 ===");
            StudentBookQuery query3 = new StudentBookQuery();
            query3.setStudentNameLike("李");
            query3.setBookNames(new String[]{"计算机网络教程", "Web安全技术", "Java Web程序设计"});
            List<Book> specificBooks = bookDao.findBooksWithSpecificNames(query3);
            System.out.println("查询结果：");
            specificBooks.forEach(book -> System.out.println("- " + book));

        } catch (Exception e) {
            System.err.println("程序运行出错: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

